﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraSplashScreen;
using FileManager_Client.BusinessLogic.FileManagerUserService;
using FileManager_Client.PresentationLayer.Common;
using FileManager_Client.Utility;
using Paway.Windows.Forms;

namespace FileManager_Client.PresentationLayer.NewUi
{
    public partial class LoginForm : BaseForm
    {
        #region Form Constructor
        public LoginForm()
        {
            InitializeComponent();
            Initialization();
            AutoLogin = true;
        }

        private void CheckAutoLogin()
        {
            var autoLogin = UserLocalFile.ReadUserAppConfig();
            if (null == autoLogin) return;
            switch (autoLogin.AutoLogin)
            {
                case AutoLoginEnum.RemeberPass:
                    Cb_RemeberPass.Checked = true;
                    break;
                case AutoLoginEnum.AutoLogin:
                    Cb_AutoLogin.Checked = true;
                    Cb_RemeberPass.Checked = true;
                    break;
            }
            Tb_Password.Text = autoLogin.PassWord;
            Tb_UserName.Text = autoLogin.UserName;
            if (autoLogin.AutoLogin != AutoLoginEnum.AutoLogin) return;
            Hide();
            var loginLoading = new LoginLoading(this);
            loginLoading.ShowDialog();
        }

        private bool AutoLogin { get; set; }
        public LoginForm(bool isAutoLogin)
        {
            InitializeComponent();
            Initialization();
            AutoLogin = isAutoLogin;
        }

        private void Initialization()
        {
            if (null == CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey))
                CacheGlobal.Add(GlobalString.System_Config_String.Wcf_FileServiceCacheKey, new FileManager_UserServiceClient());
          //  Tt_Message.AutoPopDelay = 5000;
            Tt_Message.ShowShadow = true;
            Tt_Message.Rounded = true;
            Tt_Message.ShowBeak = true;
            Tt_Message.SetToolTipIconType(Tb_UserName,ToolTipIconType.Information);
            //  Tt_Message.Active = true;
        }

        #endregion

        #region Control Event
        private void Btn_Cancel_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void Btn_Login_Click(object sender, EventArgs e)
        {
            Tt_Message.HideHint();
            if (string.IsNullOrEmpty(Tb_Password.Text.Trim()))
            {
                Tt_Message.ShowHint("请输入密码!", Tb_Password, ToolTipLocation.BottomCenter);
            }
            else
            {
                if (string.IsNullOrEmpty(Tb_UserName.Text.Trim()))
                {
                    Tb_UserName.Focus();
                    Tt_Message.ShowHint("请输入用户名!", Tb_UserName, ToolTipLocation.BottomCenter);
                }
                else
                    LoginEvent();
            }
        }

        private void Cb_AutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            var checkBox = sender as QQCheckBox;
            if (null != checkBox && checkBox.Checked)
                Cb_RemeberPass.Checked = true;
        }

        private void Tb_UserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            Tt_Message.HideHint();
            if (e.KeyChar != 13) return;
            if (string.IsNullOrEmpty(Tb_UserName.Text.Trim()))
            {
                Tt_Message.ShowHint("请输入用户名!", Tb_UserName,ToolTipLocation.BottomCenter);
            }
            else
            {
                if (string.IsNullOrEmpty(Tb_Password.Text.Trim()))
                    Tb_Password.Focus();
                else
                    LoginEvent();
            }
        }

        private void Tb_Password_KeyPress(object sender, KeyPressEventArgs e)
        {
            Tt_Message.HideHint();
            if (e.KeyChar != 13) return;
            if (string.IsNullOrEmpty(Tb_Password.Text.Trim()))
            {
                Tt_Message.ShowHint("请输入密码!", Tb_Password,ToolTipLocation.BottomCenter);
            }
            else
            {
                if (string.IsNullOrEmpty(Tb_UserName.Text.Trim()))
                {
                    Tb_UserName.Focus();
                    Tt_Message.ShowHint("请输入用户名!", Tb_UserName, ToolTipLocation.BottomCenter);
                }
                else
                    LoginEvent();
            }
        }
        #endregion

        #region Public Method
        private void LoginEvent()
        {
            Hide();
            var loginLoading = new LoginLoading(this);
            loginLoading.ShowDialog();
        }

        public void ClearLoginInfo()
        {
            Tb_UserName.Text = "";
            Tb_Password.Text = "";
        }
        #endregion

        #region Public Property
        public string LoginName
        {
            get { return Tb_UserName.Text; }
        }

        public string LoginPass
        {
            get { return Tb_Password.Text; }
        }

        public bool IsRemeberPass
        {
            get { return Cb_RemeberPass.Checked; }
        }

        public bool IsAutoLogin
        {
            get { return Cb_AutoLogin.Checked; }
        }
        #endregion

        #region Form Event
        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }

        private void LoginForm_Shown(object sender, EventArgs e)
        {
            if (!AutoLogin) return;
            CheckAutoLogin();
        }

        private void LoginForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            Tt_Message.HideHint();
            if (string.IsNullOrEmpty(Tb_Password.Text.Trim()))
            {
                Tt_Message.ShowHint("请输入密码!", Tb_Password, ToolTipLocation.BottomCenter);
            }
            else
            {
                if (string.IsNullOrEmpty(Tb_UserName.Text.Trim()))
                {
                    Tb_UserName.Focus();
                    Tt_Message.ShowHint("请输入用户名!", Tb_UserName, ToolTipLocation.BottomCenter);
                }
                else
                    LoginEvent();
            }
        }
        #endregion

    }
}